package com.sskj.api.controller.admin;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sskj.api.domain.Permission;
import com.sskj.api.domain.SysUser;
import com.sskj.api.service.ISysUserService;
import com.sskj.api.vo.req.AdminUserAddReq;
import com.sskj.api.vo.req.ModifyPasswordReq;
import com.sskj.api.vo.req.SysUserPageListReq;
import com.sskj.common.dto.TreeNode;
import com.sskj.common.util.ApiResponseUtil;
import com.sskj.common.vo.ApiResponse;
import com.sskj.common.vo.SimplePagination;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Tag(name = "管理用户模块")
@RestController
@RequestMapping("/admin/sysUser")
@Validated
public class SysUserController {

    @Autowired
    private ISysUserService adminUserService;

    @Operation(summary = "用户详情")
    @GetMapping("info")
    public ApiResponse<SysUser> info(@RequestParam(name = "id", required = false) Long id) {
        if (id == null || id <= 0) {
            id = StpUtil.getLoginIdAsLong();
        }
        SysUser info = adminUserService.info(id);

        return ApiResponseUtil.success(info);
    }

    @Operation(summary = "分页列表")
    @GetMapping("pageList")
    public ApiResponse<SimplePagination<SysUser>> pageList(SysUserPageListReq req) {
        IPage<SysUser> adminUserIPage = adminUserService.pageList(req);

        return ApiResponseUtil.success(adminUserIPage);
    }

    @Operation(summary = "新增用户")
    @PostMapping("add")
    public ApiResponse<Object> add(@Valid @RequestBody AdminUserAddReq req) {
        adminUserService.add(req);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "获取用户菜单")
    @GetMapping("menus")
    public ApiResponse<List<TreeNode<Permission>>> menus(@RequestParam(name = "id", required = false) Long id) {
        if (id == null) {
            id = StpUtil.getLoginIdAsLong();
        }
        List<TreeNode<Permission>> menus = adminUserService.getMenus(id);

        return ApiResponseUtil.success(menus);
    }

    @Operation(summary = "获取用户操作权限集合")
    @GetMapping("actions")
    public ApiResponse<List<Permission>> actions(@RequestParam(name = "id", required = false) Long id) {
        if (id == null) {
            id = StpUtil.getLoginIdAsLong();
        }
        List<Permission> actions = adminUserService.getActionPermission(id);

        return ApiResponseUtil.success(actions);
    }

    @Operation(summary = "重置密码")
    @PostMapping("resetPassword")
    public ApiResponse<Object> resetPassword(@RequestParam Long id) {
        adminUserService.resetPassword(id);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "修改密码")
    @PostMapping("modifyPassword")
    public ApiResponse<Object> modifyPassword(@Valid @RequestBody ModifyPasswordReq req) {
        long loginUserId = StpUtil.getLoginIdAsLong();
        if (adminUserService.modifyPassword(loginUserId, req.getOldPassword(), req.getNewPassword())) {
            StpUtil.logout();

            return ApiResponseUtil.success();
        }

        return ApiResponseUtil.fail("修改密码失败");
    }
}
